package com.goldsprite.gsgengine.gsgdxf.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.goldsprite.gsgengine.core.Enableable;
import com.goldsprite.gsgengine.core.EnableableGameLoop;
import com.goldsprite.gsgengine.core.managers.GameObjectManager;
import com.goldsprite.utils.math.Vector2;

/**
 * 创建:
 * <br/>- 设置视口, 重写该方法: initViewport(){ setViewport(...) }
 * <br/>- 添加事件处理器: getImp().addInputProcessor(...)
 */

public abstract class Scene extends EnableableGameLoop {
	private Viewport viewport;
	private final Vector2 viewCenter = new Vector2();
	private final Vector2 viewSize = new Vector2();
	private final Vector2 graphicSize = new Vector2();
	protected SceneManager sceneManager;
	protected GameObjectManager gameObjectManager;
	protected  InputMultiplexer imp;
	protected boolean initialized = false;

	public Scene() {
	}

	public void initialize() {
		if (initialized) return;
		viewport = sceneManager.getViewport();
		gameObjectManager = new GameObjectManager();
		start();
		initialized = true;
	}

	public boolean isInitialized() {
		return initialized;
	}

	public SceneManager getSceneManager() {
		return sceneManager;
	}

	public void setSceneManager(SceneManager sceneManager) {
		this.sceneManager = sceneManager;
	}

	public InputMultiplexer getImp() {
		return imp;
	}

	public void setImp(InputMultiplexer imp) {
		this.imp = imp;
	}

	public Vector2 getViewSize() {
		viewSize.set(viewport.getWorldWidth(), viewport.getWorldHeight());
		return viewSize;
	}

	public Vector2 getViewCenter() {
		viewCenter.set(viewport.getWorldWidth() / 2, viewport.getWorldHeight() / 2);
		return viewCenter;
	}

	public Vector2 getGraphicSize() {
		graphicSize.set(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		return graphicSize;
	}

	public Vector2 screenToWorldCoord(Vector2 screenCoord) {
		// 将反转后的屏幕坐标转换为世界坐标
		Vector3 worldCoordinates = new Vector3(screenCoord.x, screenCoord.y, 0);
		viewport.unproject(worldCoordinates);
		// 获取转换后的世界坐标
		screenCoord.x = worldCoordinates.x;
		screenCoord.y = worldCoordinates.y;
		return screenCoord;
	}

	public Vector2 worldToScreenCoord(Vector2 worldCoord) {
		// 将反转后的屏幕坐标转换为世界坐标
		Vector3 screenCoordinates = new Vector3(worldCoord.x, worldCoord.y, 0);
		viewport.project(screenCoordinates);
		// 获取转换后的世界坐标
		worldCoord.x = screenCoordinates.x;
		worldCoord.y = screenCoordinates.y;
		return worldCoord;
	}

	@Override
	public void enable() {
		super.enable();
		getSceneManager().enableInput(getImp());
	}

	@Override
	public void update(float delta) {
		ScreenUtils.clear(1f, 1f, 1f, 1f);
	}

	@Override
	public void disable() {
		super.disable();
		getSceneManager().disableInput(getImp());
	}

	@Override
	public void destroy() {
	}

	public void resize(int width, int height) {
		if (viewport != null)
			viewport.update(width, height, true);
	}

	public void pause() {
	}

	public void resume() {
	}
}
